package com.example.zhihuhelpapp.base;

import android.app.ProgressDialog;
import android.os.Bundle;

import com.example.zhihuhelpapp.api.mvp.BaseContract;
import com.example.zhihuhelpapp.api.mvp.RxPresenter;
import com.example.zhihuhelpapp.utils.UIUtils;
import com.trello.rxlifecycle.LifecycleTransformer;

/**
 * 有网络请求的页面
 */
public abstract class MyBaseRxActivity<T extends RxPresenter> extends BaseActivity implements BaseContract.BaseView {
    protected T mPresenter;
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        attachView();
        super.onCreate(savedInstanceState);

    }

    /**
     * [此方法不可再重写]
     */
    protected final void attachView() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
            if (mPresenter != null) {
                mPresenter.attachView(this);
            }
        }
    }

    protected abstract T createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void showError(String message) {
        UIUtils.showToast(message);
    }

    @Override
    public void showProgress() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
        }
        if (progressDialog.isShowing()) {
            return;
        }
        progressDialog.show();
    }

    @Override
    public void hideProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.hide();
        }
    }

    @Override
    public <T> LifecycleTransformer<T> bind() {
        return bindToLifecycle();
    }
}
